package com.person.yhzs.util;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    private static final String[] DATE_PATTERNS = {"yyyy-MM-dd", "yyyy年MM月dd日", "yyyy/MM/dd"};
    public static final Date THIS_CENTURY_END = getCenturyEnd();
    /**
     * 年月日时分秒字符串 格式模板:yyyyMMddHHmmss
     */
    public static final String CURR_TIME = "yyyyMMddHHmmss";

    public static Date currentTime() {
        return new Date();
    }

    /**
     * @return 昨天0点
     */
    public static Date yesterday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * @return 今天0点
     */
    public static Date today() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * @return 今天0点
     */
    public static Date todayEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    /**
     * 明天0点
     */
    public static Date tomorrow() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    
    /**
     * 获取指定时间
     * @param date 如果为null，默认当前时间
     */
    public static Date getTheDate(Date date, int field, int amount) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
        	cal.setTime(date);
        }
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(field, amount);
        return cal.getTime();
    }

    /**
     * Try to convert string to date
     */
    public static Date stringToDate(String str) {
        try {
            return DateUtils.parseDate(str, DATE_PATTERNS);
        } catch (Exception ignored) {
        }
        return null;
    }

    /**
     * Convert date to string
     * pattern: yyyy-MM-dd
     */
    public static String dateToString(Date date) {
        try {
            return DateFormatUtils.format(date, "yyyy-MM-dd");
        } catch (Exception ignored) {
        }
        return null;
    }
    
    /**
     * Convert date to string
     * pattern: yyyy-MM-dd'T'HH:mm:ss
     */
    public static String dateTimeToString(Date dateTime) {
    	try {
    		return DateFormatUtils.format(dateTime, "yyyy-MM-dd'T'HH:mm:ss");
    	} catch (Exception ignored) {
    	}
    	return null;
    }

    /**
     * Convert date to string
     * pattern: yyyy-MM-dd HH:mm:ss
     */
    public static String dateTimeToStr(Date dateTime) {
    	try {
    		return DateFormatUtils.format(dateTime, "yyyy-MM-dd HH:mm:ss");
    	} catch (Exception ignored) {
    	}
    	return null;
    }

    public static String dateTimeToStrYMDHM(Date dateTime) {
        try {
            return DateFormatUtils.format(dateTime, "yyyy-MM-dd HH:mm");
        } catch (Exception ignored) {
        }
        return null;
    }

    private static Date getCenturyEnd() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(2099, 11, 31, 23, 59, 59);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
    }
    
    public static Date getBeginOfCurrentMonth() {
    	Calendar cal = Calendar.getInstance();
    	cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

//    /**
//     * 返回指定日期的YYYYMMDD构成的时间整数
//     * @param date 如果日期为空，使用当前日期作为默认日期
//     * @return
//     */
//    public static int getYYYYMMDD(Date date) {
//        Calendar cal = Calendar.getInstance();
//        if (date != null) {
//            cal.setTime(date);
//        }
//        String curDate = cal.get(Calendar.YEAR) +
//                CommonUtil.formatStrToLen(String.valueOf(cal.get(Calendar.MONTH) + 1), 2, '0') +
//                CommonUtil.formatStrToLen(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)), 2, '0');
//        return Integer.parseInt(curDate);
//    }

    /**
     * 从日历中返回以分钟为单位的小时x60+分钟数。
     * @param date
     * @return
     */
    public static int getHMFromDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE);
    }

    public static String getDateStringFromYYYYMMDD(int date) {
        String dateStr = String.valueOf(date);
        if (dateStr.length() < 8) {
            return null;
        }
        return new StringBuilder().append(dateStr,0, 4).append("-")
                .append(dateStr, 4, 6).append("-")
                .append(dateStr, 6, 8).toString();
    }

    public static Date getBeginOfTheWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static Date getBeginOfTheMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static Date getBeginOfTheYear(Date date) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.set(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static Date getEndOfTheWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    public static Date getEndOfTheMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);

        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    /**
     * 获取年/月/周的最后一天
     * @param date
     * @param ymw Calendar中的YEAR/MONTH/WEEK的值
     * @param dayOfYMW Calendar中的Day_of_YEAR/day_of_MONTH/day_of_WEEK的值
     * @return
     */
    public static Date getEndOfTheYMW(Date date, int ymw, int dayOfYMW) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.set(dayOfYMW, 1);
        cal.add(ymw, 1);
        cal.add(dayOfYMW, -1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    public static String getChineseDateString(){
        Calendar calendar=Calendar.getInstance();
        return calendar.get(Calendar.YEAR)+"年"
                +(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日"
                +calendar.get(Calendar.HOUR_OF_DAY)+"时"+calendar.get(Calendar.MINUTE)+"分"
                +calendar.get(Calendar.SECOND)+"秒";
    }
    public static String getCurrTime() {
        Date _date = new Date(System.currentTimeMillis());
        DateFormat df = new SimpleDateFormat(CURR_TIME);
        return df.format(_date);
    }
}
